var webpack = require('webpack'),
    HtmlWebpackPlugin = require('html-webpack-plugin'),
    path = require('path');
//定义了一些文件夹的路径
var ROOT_PATH = path.resolve(__dirname);
var APP_PATH = path.resolve(ROOT_PATH, 'app');
var LIB_PATH = path.resolve(ROOT_PATH, 'lib');
var BUILD_PATH = path.resolve(ROOT_PATH, 'build');
var appDir = path.resolve(process.cwd(), 'app'), // process.cwd()：返回运行当前脚本的工作目录的路径
    imagePath = path.resolve(appDir, 'img'),
    scssPath = path.resolve(appDir, 'scss'),
    corePath = path.resolve(appDir, 'core'),
    configPath = path.resolve(appDir, 'config'),
    viewsPath = path.resolve(appDir, 'views');

module.exports = {
    //项目的文件夹 可以直接用文件夹名称 默认会找index.js 也可以确定是哪个文件名字
    entry: {
        app: [path.resolve(APP_PATH, 'views/index.jsx')]
    },
    //输出的文件名 合并以后的js会命名为bundle.js
    output: {
        path: BUILD_PATH,
        publicPath: '/',
        filename: '[name].js'
    },
    //resolve的参数，把jsx这种扩展名添加进去，这样就可以在js中import加载jsx这种扩展名的脚本
    resolve: {
        extensions: ['', '.js', '.jsx'],
        alias: { //配置别名，在项目中可缩减引用路径
            'img': imagePath,
            'scss': scssPath,
            'core': corePath,
            'config': configPath,
            'views': viewsPath,
        },
    },
    devServer: {
        historyApiFallback: true,
        hot: true,
        inline: true,
        host: '0.0.0.0',
        port: 7000
    },
    module: {
        loaders: [{
            test: /\.js[x]?$/,
            include: APP_PATH,
            loader: 'babel-loader',
            query: {
                //添加两个presents 使用这两种presets处理js或者jsx文件
                presets: ['react', 'es2015'],
                env: {
                    development: {
                        presets: ["react-hmre"]
                    }
                }
            }
        }, {
            test: /\.js$/,
            loader: "babel-loader",
            query: {
                presets: ['es2015']
            }
        }, {
            test: /\.(png|jpg|gif)$/,
            loader: "url-loader"
        }, {
            test: /\.scss$/,
            loaders: ['style', 'css?sourceMap', 'sass?sourceMap'],
            include: APP_PATH
        }]
    },
    //添加我们的插件 会自动生成一个html文件
    plugins: [
        new webpack.DllReferencePlugin({ // 加快webpack打包速度
            context: __dirname,
            manifest: require('./build/vendor-manifest.json')
        }),
        new HtmlWebpackPlugin({
            template: path.resolve(__dirname, 'app/index.html'),
            inject: true
        })
    ]
};